﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using EuroManager.Domain.Simulator.Helpers;

namespace EuroManager.Domain.Simulator.Engine.Movements
{
    public class StillMovement : IMovement
    {
        private Vector2D location;
        private Vector2D velocity;
        private float deceleration;
        
        public StillMovement(Vector2D location, Vector2D velocity, float deceleration)
        {
            Contract.Requires(deceleration.IsRational() && deceleration >= 0);

            this.location = location;
            this.velocity = velocity;
            this.deceleration = deceleration;
        }

        public bool IsFinished
        {
            get { return false; }
        }

        public Vector2D Location
        {
            get { return location; }
        }

        public Vector2D Velocity
        {
            get { return velocity; }
        }

        public void Update(float elapsedTime)
        {
            if (!velocity.IsZero)
            {
                Vector2D newVelocity = Physics.Velocity(velocity, -velocity.Normalized() * deceleration, elapsedTime);

                if (Vector2D.Dot(velocity, newVelocity) < 0)
                {
                    // Velocity has been reversed - stop moving immediately.
                    velocity = Vector2D.Zero;
                }
                else
                {
                    velocity = newVelocity;
                    location = Physics.Location(location, velocity, elapsedTime);
                }
            }
        }
    }
}
